package LuceneIndexCreate.http;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;

/**
 * Created by User on 2017/12/6.
 */
public class Http {

    public static String sendGet(String url) {
        String result = "";
        BufferedReader in = null;
        try {
            String urlNameString = url;
            URL realUrl = new URL(urlNameString);

            // 打开和URL之间的连接
            URLConnection connection = realUrl.openConnection();
            // 设置通用的请求属性
            connection.setRequestProperty("accept", "*/*");
            connection.setRequestProperty("connection", "Keep-Alive");
            connection.setRequestProperty("user-agent",
                    "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36");
            connection.setRequestProperty("referer", "https://www.1688.com/");
            connection.setRequestProperty("Cookie", "__sw_newuod_count__=1; _bl_uid=FRjhpaX9vqb29a29kmU4uaC0Im6h; __sw_newuno_count__=1; _uab_collina=151273443773292311180325; ali_beacon_id=115.196.139.213.1475666246706.714021.4; l=Al1dZXp9JRnD9VEAaq/RH15p7TNWQJHZ; cna=jCFpEFLgvRACAbedRVqSkA0b; UM_distinctid=15fb5a5d1ccb35-0bb6cd1567205f-5e193d17-1fa400-15fb5a5d1cd991; webp=1; _m_h5_tk=d481e47db5425d05d2ba2a05850bb509_1512541437118; _m_h5_tk_enc=d5da258dabf7541a9fec4adba884d4ab; ali_apache_track=\"c_ms=1|c_mid=b2b-2161592039|c_lid=%E6%9C%B1%E4%B9%BE%E5%8A%9B\"; _cn_slid_=y7kBAiAWJg; last_mid=b2b-2161592039; __last_loginid__=\"%E6%9C%B1%E4%B9%BE%E5%8A%9B\"; _umdata=0712F33290AB8A6D64100FA911E6879425991546EA056039331FBB13348060C431704403519010BECD43AD3E795C914CA596D64F1F255BD46B1D9FEBF4468FF9; h_keys=\"%u660e%u661f%u7eaa%u5ff5%u54c1#%u7b14%u8bb0%u672c#%u53e4%u8463%u949f%u8868#%u7535%u6e90%u9002%u914d%u5668#%u789f\\\\\\\\%u7535%u6c60\\\\\\\\%u7535%u6e90%u9002%u914d%u5668#%u789f\\\\%u7535%u6c60\\\\%u7535%u6e90%u9002%u914d%u5668#%u4e2d%u88e4/%u4e94%u5206%u88e4#%u4e2d%u88e4\\\\/%u4e94%u5206%u88e4#%u7b14%u8bb0%u672c%u7535%u8111#%u789f\"; JSESSIONID=xc0ZxMc-b9qYafMR9nm02F9Yd8-KFGeHdQ-hq4N; alicnweb=touch_tb_at%3D1512796141445%7Clastlogonid%3D%25E6%259C%25B1%25E4%25B9%25BE%25E5%258A%259B%7ChomeIdttS%3D15686904835935605040946498468641069254%7ChomeIdttSAction%3Dtrue%7Cshow_inter_tips%3Dfalse; __cn_logon__=false; _tmp_ck_0=\"7yvliT2OxKpgXqraKKg1IaLd5gcF9UJ81P1aQFR%2BUvpgTujLaZtiLK%2F3SeUn%2Bbb6RyHMcdudgVZqF8%2BlrbrmVk%2Fx6Z2MbusCaYpy%2BjqlufxYLcAqb25w4%2B6rQDXBR2Sfi9LiQ7mGyJ%2BwNc3Osk%2B3SVSzHGfp89Ip%2FfqismQO%2BbR79xILrKqqTE0pphj9siRpZBOWY%2FrKbaygQLABOwqfoKvOW4xgDR2BTxbfkzysLvVrQHizqAZ24P0CKJRuj7waQh8Cg8lKvAY5NYimoCE%2Bjz0CNUV2dcMVbkT53sN4TOybiTnehW%2FvbQ9iUaN5VER5odGL%2FEgT3g3pzWOnb3CvFPJs2UQmL6JaeTAgbBa6jXxR3ED%2Bq743LxFVL3cP%2B%2FjA8Qb9gyjjwww%3D\"; alisw=swIs1200%3D1%7C; ali_ab=115.196.139.213.1475647300457.1; ad_prefer=\"2017/12/09 13:14:33\"; isg=ApeXulF2gcVEiAiWOL6FLBCBJgshdGXULrEAZ-nE82bNGLda8az7jlV6Do79");
            // 建立实际的连接
            connection.connect();
            // 获取所有响应头字段
            Map<String, List<String>> map = connection.getHeaderFields();
            // 遍历所有的响应头字段
            for (String key : map.keySet()) {
                //System.out.println(key + "--->" + map.get(key));
            }
            // 定义 BufferedReader输入流来读取URL的响应
            in = new BufferedReader(new InputStreamReader(
                    connection.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            System.out.println("发送GET请求出现异常！" + e);
            e.printStackTrace();
        }
        // 使用finally块来关闭输入流
        finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
        return result;
    }

    public static String sendGetMobile(String url, String cookies) {
        String result = "";
        BufferedReader in = null;
        try {
            String urlNameString = url;
            URL realUrl = new URL(urlNameString);

            // 打开和URL之间的连接
            HttpURLConnection connection = (HttpURLConnection) realUrl.openConnection();
            // 设置通用的请求属性
            connection.setRequestProperty("accept", "*/*");
            connection.setRequestProperty("connection", "Keep-Alive");
            connection.setRequestProperty("user-agent",
                    "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Mobile Safari/537.36");
            connection.setRequestProperty("referer", "https://www.1688.com/");
            connection.setRequestProperty("Cookie", cookies);
            // 建立实际的连接
            connection.connect();
            // 获取所有响应头字段
            int responseCode = connection.getResponseCode();
            if (responseCode == 302) {
                String location302 = connection.getHeaderField("Location");
                String cookies302 = connection.getHeaderField("Set-Cookie");
                return sendGetMobile(location302, cookies302);
            } else {
                // 定义 BufferedReader输入流来读取URL的响应
                in = new BufferedReader(new InputStreamReader(
                        connection.getInputStream()));
                String line;
                while ((line = in.readLine()) != null) {
                    result += line;
                }
                return result;
            }
        } catch (Exception e) {
            System.out.println("发送GET请求出现异常！" + e);
            e.printStackTrace();
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }

        return null;
    }

    public static void main(String[] args) throws IOException {




    }

}
